﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Models;

namespace DAL
{
    public class CharInfoService
    {
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="id">角色id</param>
        /// <returns></returns>
        public Characters GetCharInfo(string id)
        {
            Characters cha=new Characters();
            try
            {
                string sql = string.Format("select * from characters where objid='{0}'", id);
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                using (MySqlDataReader reader = com.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        cha.Account_name = Convert.ToString(reader["account_name"]);
                        cha.Char_name = Convert.ToString(reader["char_name"]);
                        cha.MaxHp = Convert.ToInt32(reader["maxhp"]);
                        cha.CurHp = Convert.ToInt32(reader["curhp"]);
                        cha.MaxMp = Convert.ToInt32(reader["maxmp"]);
                        cha.CurMp = Convert.ToInt32(reader["curmp"]);
                        cha.Ac = Convert.ToInt32(reader["ac"]);
                        cha.Str = Convert.ToInt32(reader["str"]);
                        cha.Con = Convert.ToInt32(reader["con"]);
                        cha.Dex = Convert.ToInt32(reader["dex"]);
                        cha.Intel = Convert.ToInt32(reader["intel"]);
                        cha.Wis = Convert.ToInt32(reader["wis"]);
                        cha.Cha = Convert.ToInt32(reader["cha"]);
                        cha.Type = Convert.ToInt32(reader["type"]);
                        cha.Mapid = Convert.ToInt32(reader["mapid"]);
                        cha.Food = Convert.ToInt32(reader["food"]);
                        cha.Lawful = Convert.ToInt32(reader["lawful"]);
                        cha.ClanID = Convert.ToInt32(reader["ClanID"]);
                        cha.ClanName = Convert.ToString(reader["clanname"]);
                        cha.Sex = Convert.ToInt32(reader["sex"]);
                    }
                }
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return cha;
        }

        /// <summary>
        /// 根据地图编号查询地图名称
        /// </summary>
        /// <param name="mapid">地图编号</param>
        /// <returns></returns>
        public string GetMapNameBYMapId(int mapid)
        {
            string mapName = string.Empty;
            try
            {
                string sql = string.Format("select locationname from mapids where mapid={0} ", mapid);
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                mapName = com.ExecuteScalar().ToString();
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return mapName;
        }

        public DataSet GetAllItem(string id,int type)
        {
            //查询角色物品
            DataSet ds = new DataSet();
            try
            {
                string sql = string.Empty;
                switch(type)
                {
                    case 1:
                        //背包
                        sql = string.Format("select id,item_name,count,char_id from character_items where char_id={0}", id);
                        break;
                    case 2:
                        //仓库
                        sql = string.Format("select id,account_name,item_name,count from character_warehouse where account_name=(select account_name from characters where objid={0})", id);
                        break;
                    case 3:
                        //妖仓
                        sql = string.Format("select id,char_id,item_name,count from character_elf_warehouse where char_id={0}", id);
                        break;
                    case 4:
                        //盟仓
                        sql = string.Format("select id,item_name,count from clan_warehouse where clan_name='{0}'", id);
                        break;
                }
                MySqlDataAdapter objAda = new MySqlDataAdapter(sql, DBHelper.con);
                objAda.Fill(ds, "CharItem");
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// 根据血盟id查询血盟名称
        /// 目前用不到了
        /// </summary>
        /// <param name="id">血盟id</param>
        /// <returns></returns>
        public string GetClanNameBYClanID(string id)
        {
            string clanName = string.Empty;
            try
            {
                string sql = string.Format("select clan_name from clan_data where clan_id={0} ", id);
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                clanName = com.ExecuteScalar().ToString();
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return clanName;
 
        }

        /// <summary>
        /// 删除角色单个道具
        /// </summary>
        /// <param name="id">道具的流水号</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool DeleteItem(string id,int type)
        {
            int num = 0;
            try
            {

                string sql = string.Empty;
                switch (type)
                {
                    case 1:
                        //背包
                        sql = string.Format("delete from character_items where id={0}", id);
                        break;
                    case 2:
                        //仓库
                        sql = string.Format("delete from character_warehouse where id={0}", id);
                        break;
                    case 3:
                        //妖仓
                        sql = string.Format("delete from character_elf_warehouse where id={0}", id);
                        break;
                    case 4:
                        //盟仓
                        sql = string.Format("delete from clan_warehouse where id={0}", id);
                        break;
                }
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                num = Convert.ToInt32(com.ExecuteNonQuery());
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (num == 0)
            {
                return false;
            }
            return true;
 
        }

        /// <summary>
        /// 删除角色全部道具
        /// </summary>
        /// <param name="id">角色编号/血盟名称</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool AllDeleteItem(string id, int type)
        {
            int num = 0;
            try
            {

                string sql = string.Empty;
                switch (type)
                {
                    case 1:
                        //背包
                        sql = string.Format("delete from character_items where char_id={0}", id);
                        break;
                    case 2:
                        //仓库
                        sql = string.Format("delete from character_warehouse where account_name='{0}'", id);
                        break;
                    case 3:
                        //妖仓
                        sql = string.Format("delete from character_elf_warehouse where char_id={0}", id);
                        break;
                    case 4:
                        //盟仓
                        sql = string.Format("delete from clan_warehouse where clan_name='{0}'", id);
                        break;
                }
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                num = Convert.ToInt32(com.ExecuteNonQuery());
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (num == 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 逐个修改角色信息
        /// </summary>
        /// <param name="a">角色名称</param>
        /// <param name="type">修改类型</param>
        /// <returns></returns>
        public bool CharAux(string a,int type)
        {
            int num = 0;
            try
            {
                string sql = string.Empty;
                switch (type)
                {
                    case 1:
                        sql = string.Format("update characters set AccessLevel=0 where char_name='{0}'", a);
                        break;
                    case 2:
                        sql = string.Format("update characters set AccessLevel=100 where char_name='{0}'", a);
                        break;
                    case 3:
                        sql = string.Format("update characters set AccessLevel=200 where char_name='{0}'", a);
                        break;
                    case 4:
                        sql = string.Format("update characters set LocX=33439,LocY=32800,MapID=4 where char_name='{0}'", a);
                        break;
                    case 5:
                        sql = string.Format("update characters set maxhp=maxhp+100,maxmp=maxmp+100 where char_name='{0}'", a);
                        break;
                    case 6:
                        sql = string.Format("update characters set ac=ac-10 where char_name='{0}' and ac>=-117", a);
                        break;
                    case 7:
                        sql = string.Format("update characters set LocX=32736,LocY=32798,MapID=99 where char_name='{0}'", a);
                        break;
                    case 8:
                        sql = string.Format("update characters set lawful=32767 where char_name='{0}'", a);
                        break;
                    case 9:
                        sql = string.Format("update characters set food=225 where char_name='{0}'", a);
                        break;
                    case 10:
                        sql = string.Format("update characters set level=99 where char_name='{0}'", a);
                        break;
                    case 11:
                        sql = string.Format("update characters set CurHp={0} where char_name='{1}'", GetMaxHp(a), a);
                        break;
                    case 12:
                        sql = string.Format("update characters set Curmp={0} where char_name='{1}'",GetMaxMp(a), a);
                        break;
                }
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                num = Convert.ToInt32(com.ExecuteNonQuery());
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (num == 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 查出血的上限
        /// </summary>
        /// <param name="name">角色名称</param>
        /// <returns></returns>
        private int GetMaxHp(string name)
        {
            int maxHp = 0;
            try
            {
                string sql = string.Format("select maxhp from characters where char_name='{0}'", name);
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                maxHp = Convert.ToInt32(com.ExecuteScalar());
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (maxHp != 0)
            {
                return maxHp;
            }
            return maxHp=0;
        }

        /// <summary>
        /// 查出魔的上限
        /// </summary>
        /// <param name="name">角色名称</param>
        /// <returns></returns>
        private int GetMaxMp(string name)
        {
            int maxMp = 0;
            try
            {
                string sql = string.Format("select maxmp from characters where char_name='{0}'", name);
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                maxMp = Convert.ToInt32(com.ExecuteScalar());
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (maxMp != 0)
            {
                return maxMp;
            }
            return maxMp = 0;
        }

        /// <summary>
        /// 修改玩家信息
        /// </summary>
        /// <param name="str"></param>
        /// <param name="con"></param>
        /// <param name="dex"></param>
        /// <param name="cha"></param>
        /// <param name="intel"></param>
        /// <param name="wis"></param>
        /// <param name="sex"></param>
        /// <param name="name">角色名称</param>
        /// <returns></returns>
        public bool SetCharInfo(int str, int con, int dex, int cha, int intel, int wis, int sex,string name)
        {
            int num = 0;
            try
            {
                string sql = string.Format("update characters set str={0},con={1},dex={2},cha={3},intel={4},wis={5},sex={6} where char_name='{7}'", str, con, dex, cha, intel, wis, sex, name);
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                num = Convert.ToInt32(com.ExecuteNonQuery());
            }
            catch
            {
                Error.s = "读取数据库时发生错误！";
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            if (num == 0)
            {
                return false;
            }
            return true;
        }
    }
}
